package com.ktjy.service.impl;

import com.ktjy.entity.SysUser;
import com.ktjy.mapper.SysUserMapper;
import com.ktjy.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("sysUserService")
@Transactional(isolation= Isolation.DEFAULT,propagation= Propagation.REQUIRED,
        readOnly=false,timeout=-1)
public class SysUserServiceImpl implements SysUserService {
    // 定义 Mapper
    @Autowired
    private SysUserMapper sysUserMapper;

    // 提供 setter 方法用于注入属性
    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @Override
    public List<SysUser> getSysUserList(SysUser sysUser) {
        return sysUserMapper.getSysUserList(sysUser);
    }

    @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,
            readOnly=false,timeout=-1)
    @Override
    public int saveBatch(List<SysUser> sysUserList) {
        int rows = 0;
        for (int i = 0; i < sysUserList.size(); i++) {
            // 拿出每一条用户数据插入
            SysUser sysUser = sysUserList.get(i);
            if (i == 1) {
                // 插入第二条数据时，模拟数据库异常，查看事务是否生效
                throw new RuntimeException("数据库异常");
            }
            // 调用插入数据操作，计入总受影响行数
            rows += sysUserMapper.insert(sysUser);
        }
        // 返回总受影响行数
        return rows;
    }

    @Override
    public int add(SysUser sysUser) {
        return sysUserMapper.add(sysUser);
    }

    @Override
    public int update(SysUser sysUser) {
        return sysUserMapper.update(sysUser);
    }

    @Override
    public int delete(SysUser sysUser) {
        return sysUserMapper.delete(sysUser);
    }
}
